#ifndef QDEVICEMANAGER_H
#define QDEVICEMANAGER_H

#include "devicelibglobal.h"

#include <QObject>
#include <QList>
#include <QMap>
#include <QTimer>
#include <QDateTime>

struct tagDeviceInfo;
struct tagServerInfo;

class QDeviceServer;
class QServerUpdate;
class QServerDataSync;
class QServerDriverListern;
class QSendFile;

class DEVICELIB_EXPORT QDeviceManager : public QObject
{
    Q_OBJECT
public:
    explicit QDeviceManager(tagServerInfo* info,QObject *parent = 0);
    ~QDeviceManager();

    tagDeviceInfo*      get_device(const QString &uuid);
    QList<tagDeviceInfo*> get_devices();

    QServerUpdate*      get_update_server();

    QServerDataSync *   start_data_sync(const QString &uuid);
    void    end_data_sync(const QString &uuid);
    void    locked_data(const QString &device_uuid,const QString &uuid,bool locked);
    void    set_value(const QString &device_uuid,const QString &uuid,const QVariant &value);

    QServerDriverListern* start_driver_listern(const QString &uuid);

    void                start_load_database(const QString &uuid,const QString &name);
protected:
    void    recv_sync_data(const QByteArray& data);
    void    recv_listern_data(const QByteArray& data);
    void    recv_database_data(const QByteArray& data);
signals:
    void    device_offline(const QString &uuid);
    void    device_online(tagDeviceInfo* device);
    void    device_property_refreach(const QString &uuid,const QStringList &list);
    void    data_refresh(const QString &uuid);
    void    data_value_refresh(const QStringList &uuid);

    void    database_count(int count);
    void    database_next();
    void    database_error(const QString &error);
    void    database_end();
protected slots:
    void    new_device(tagDeviceInfo* device);
    void    delete_device(const QString &uuid);
    void    property_refreach(const QString &uuid,const QStringList &list);
    void    recv(const QByteArray& data);
protected:
    QList<tagDeviceInfo*>               m_devices;
    QMap<QString,tagDeviceInfo*>        m_uuid_to_device;
    QDeviceServer                       *m_device_server;
    tagServerInfo                       *m_server_info;
    QServerUpdate                       *m_update_server;
    QMap<QString,QServerDataSync*>     m_uuid_to_data;
    QMap<QString,QServerDriverListern*> m_uuid_to_listern;
    QSendFile                           *m_send_file;
    QString                             m_database_uuid;
    int                                 m_retry;
};

#endif // QDEVICEMANAGER_H
